﻿using System;

namespace _04_StateModel
{
    class Program
    {
        static void Main(string[] args)
        {
            //紧急项目
            Work emergencyProjects = new Work();
            emergencyProjects.Hour = 9;
            emergencyProjects.WriteProgram();
            emergencyProjects.Hour = 10;
            emergencyProjects.WriteProgram();
            emergencyProjects.Hour = 12;
            emergencyProjects.WriteProgram();
            emergencyProjects.Hour = 13;
            emergencyProjects.WriteProgram();
            emergencyProjects.Hour = 14;
            emergencyProjects.WriteProgram();
            emergencyProjects.Hour = 17;

            //emergencyProjects.WorkFinished = true;
            emergencyProjects.TaskFinished = false;

            emergencyProjects.WriteProgram();
            emergencyProjects.Hour = 19;
            emergencyProjects.WriteProgram();
            emergencyProjects.Hour = 22;
            emergencyProjects.WriteProgram();

            Console.Read();
        }
    }
    // 工作
    public class Work
    {
        private State current;
        public Work()
        {
            current = new ForenoonState();
        }
        public double Hour { get; set; }
        public bool TaskFinished { get; set; }

        public void SetState(State state)
        {
            current = state;
        }
        public void WriteProgram()
        {
            current.WriteProgram(this);
        }

    }
    public abstract class State
    {
        public abstract void WriteProgram(Work work);
    }

    //上午工作状态
    public class ForenoonState : State
    {
        public override void WriteProgram(Work work)
        {
            if (work.Hour < 12)
            {
                Console.WriteLine($"当前时间：{work.Hour}点 上午工作，精神百倍");
            }
            else
            {
                // 超过12点转入中午工作状态
                work.SetState(new NoonState());
                work.WriteProgram();
            }
        }
    }
    //上午工作状态
    public class NoonState : State
    {
        public override void WriteProgram(Work work)
        {
            if (work.Hour < 13)
            {
                Console.WriteLine($"当前时间：{work.Hour}点 饿了，午饭：犯困，午休");
            }
            else
            {
                // 超过12点转入中午工作状态
                work.SetState(new AfternoonState());
                work.WriteProgram();
            }
        }
    }

    //下午和傍晚工作状态
    public class AfternoonState : State
    {
        public override void WriteProgram(Work work)
        {
            if (work.Hour < 17)
            {
                Console.WriteLine($"当前时间：{work.Hour}点 下午状态不错，继续努力");
            }
            else
            {
                // 超过17点转入下午工作状态
                work.SetState(new EveningState());
                work.WriteProgram();
            }
        }
    }

    // 晚间工作状态
    public class EveningState : State
    {
        public override void WriteProgram(Work work)
        {
            if (work.TaskFinished)
            {
                // 完成任务则转入下班状态
                work.SetState(new RestState());
                work.WriteProgram();
            }
            else
            {
                if (work.Hour < 21)
                {
                    Console.WriteLine($"当前时间：{work.Hour}点 加班哦，疲劳至极");
                }
                else
                {
                    // 超过21点转入睡眠状态
                    work.SetState(new SleepingState());
                    work.WriteProgram();
                }
            }
          
        }
    }
    // 下班休息状态
    public class RestState : State
    {
        public override void WriteProgram(Work work)
        {
            Console.WriteLine($"当前时间：{work.Hour}点 下班回家了");

        }
    }

    // 睡眠状态
    public class SleepingState : State
    {
        public override void WriteProgram(Work work)
        {
            Console.WriteLine($"当前时间：{work.Hour}点 不行了，睡着了");

        }
    }
}
